View Javadoc
1 /* ==================================================================== 2 * Bigyo Software License, version 1.1 3 * 4 * Copyright (c) 2004, Zsombor Gegesy. All rights reserved. 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * 3. Neither the name of the Bigyo Group nor the name "Bigyo" nor 18 * the names of its contributors may be used to endorse or promote 19 * products derived from this software without specific prior 20 * written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 * 35 * ==================================================================== 36 */ 37 38 package net.sf.bigyo.container; 39 40 import java.io.File; 41 import java.io.FileNotFoundException; 42 import java.io.FileReader; 43 import java.io.IOException; 44 import java.io.InputStream; 45 import java.io.InputStreamReader; 46 import java.io.Reader; 47 import java.util.ArrayList; 48 import java.util.List; 49 50 import javax.xml.parsers.FactoryConfigurationError; 51 52 import net.sf.bigyo.api.ContainerException; 53 import net.sf.bigyo.container.Main.ConfigurationChecker; 54 import net.sf.bigyo.container.config.PreGeneratedWrapperStrategy; 55 import net.sf.bigyo.container.profile.AllProfile; 56 import net.sf.bigyo.container.profile.ProfileChecker; 57 import net.sf.bigyo.container.profile.SimpleProfileChecker; 58 59 import org.apache.log4j.BasicConfigurator; 60 import org.apache.log4j.LogManager; 61 import org.apache.log4j.Logger; 62 import org.apache.log4j.PropertyConfigurator; 63 import org.apache.log4j.xml.DOMConfigurator; 64 65 /*** 66 * @author zsombor 67 * 68 * Created on 2004.10.16. 69 * Created at 21:35:15 net.sf.bigyo.container.Kernel 70 * 71 */ 72 public class Kernel { 73 74 final static Logger LOG = LogManager.getLogger(Kernel.class); 75 76 public static void main(String[] args) throws FileNotFoundException, ContainerException { 77 new Kernel(args).run(); 78 } 79 80 boolean log4jReady = false; 81 82 String log4jConfig; 83 ProfileChecker profileChecker; 84 List registries; 85 String componentDir; 86 87 String refresh = null; 88 89 int refreshTime = 60000; 90 91 Main container; 92 private boolean debugMode; 93 94 public Kernel(String[] args) { 95 SimpleProfileChecker tmpProfileChecker = null; 96 registries = new ArrayList(); 97 String log4j = null; 98 if (args != null) { 99 for (int i = 0; i < args.length; i++) { 100 if (args[i].startsWith("--profile=")) { 101 if (tmpProfileChecker == null) 102 tmpProfileChecker = new SimpleProfileChecker(); 103 104 String profile = args[i].substring("--profile=".length()); 105 tmpProfileChecker.addProfile(profile); 106 log("add profile:" + profile); 107 } 108 if (args[i].startsWith("--log4j=")) { 109 log4j = args[i].substring("--log4j=".length()); 110 log("use log4j:" + log4j); 111 } 112 if (args[i].startsWith("--registry=")) { 113 String registry = args[i].substring("--registry=".length()); 114 log("add registry:" + registry); 115 registries.add(registry); 116 117 } 118 if (args[i].startsWith("--components=")) { 119 componentDir = args[i].substring("--components=".length()); 120 log("set component directory:" + componentDir); 121 } 122 if (args[i].startsWith("--refresh=")) { 123 refresh = args[i].substring("--refresh=".length()); 124 log("set refresh to:" + refresh); 125 } 126 if (args[i].startsWith("--refresh-time=")) { 127 refreshTime = Integer.parseInt(args[i].substring("--refresh-time=".length())); 128 log("set refresh time to:" + refreshTime); 129 } 130 if (args[i].startsWith("--debug")) { 131 debugMode = true; 132 log("use debug mode"); 133 } 134 135 } 136 } 137 if (log4j!=null) 138 configureLog4j(log4j); 139 140 if (componentDir == null) 141 throw new RuntimeException("No --components specified!"); 142 profileChecker = tmpProfileChecker != null 143 ? (ProfileChecker) tmpProfileChecker 144 : (ProfileChecker) new AllProfile(); 145 } 146 147 /*** 148 * @return Returns the componentDir. 149 */ 150 public String getComponentDir() { 151 return componentDir; 152 } 153 154 /*** 155 * @return Returns the container. 156 */ 157 public Main getContainer() { 158 return container; 159 } 160 161 /*** 162 * @return Returns the log4jConfig. 163 */ 164 public String getLog4jConfig() { 165 return log4jConfig; 166 } 167 168 /*** 169 * @return Returns the profileChecker. 170 */ 171 public ProfileChecker getProfileChecker() { 172 return profileChecker; 173 } 174 175 /*** 176 * @return Returns the refresh. 177 */ 178 public String getRefresh() { 179 return refresh; 180 } 181 182 /*** 183 * @return Returns the refreshTime. 184 */ 185 public int getRefreshTime() { 186 return refreshTime; 187 } 188 189 /*** 190 * @param componentDir 191 * The componentDir to set. 192 */ 193 public void setComponentDir(String componentDir) { 194 this.componentDir = componentDir; 195 } 196 197 /*** 198 * @param log4jConfig 199 * The log4jConfig to set. 200 */ 201 public void setLog4jConfig(String log4jConfig) { 202 this.log4jConfig = log4jConfig; 203 } 204 205 /*** 206 * @param profileChecker 207 * The profileChecker to set. 208 */ 209 public void setProfileChecker(ProfileChecker profileChecker) { 210 this.profileChecker = profileChecker; 211 } 212 213 /*** 214 * @param refresh 215 * The refresh to set. 216 */ 217 public void setRefresh(String refresh) { 218 this.refresh = refresh; 219 } 220 221 /*** 222 * @param refreshTime 223 * The refreshTime to set. 224 */ 225 public void setRefreshTime(int refreshTime) { 226 this.refreshTime = refreshTime; 227 } 228 229 /*** 230 * @return Returns the debugMode. 231 */ 232 public boolean isDebugMode() { 233 return debugMode; 234 } 235 236 /*** 237 * @throws FactoryConfigurationError 238 */ 239 private void configureLog4j(String log4jConfig) throws FactoryConfigurationError { 240 this.log4jConfig = log4jConfig; 241 if (log4jConfig != null) { 242 if (log4jConfig.endsWith(".properties")) { 243 PropertyConfigurator.configure(log4jConfig); 244 log4jReady = true; 245 log("configure log4j as property file, from " + log4jConfig); 246 } else { 247 if (log4jConfig.endsWith(".xml")) { 248 DOMConfigurator.configure(log4jConfig); 249 log4jReady = true; 250 log("configure log4j as xml file, from " + log4jConfig); 251 } else { 252 BasicConfigurator.configure(); 253 log4jReady = true; 254 log("basic configuration for log4j"); 255 } 256 } 257 } 258 } 259 260 /*** 261 * @throws FileNotFoundException 262 * @throws ContainerException 263 * 264 */ 265 public void run() throws FileNotFoundException, ContainerException { 266 initContainer(); 267 startContainer(); 268 if (debugMode) { 269 System.err.println("now running...,press enter to close"); 270 try { 271 System.in.read(); 272 } catch (IOException e1) { 273 LOG.warn("stopped " + e1.getMessage(), e1); 274 } 275 container.stop(); 276 } 277 } 278 279 /*** 280 * @throws FileNotFoundException 281 * 282 */ 283 private void initContainer() throws FileNotFoundException { 284 285 for (int i = 0; i < registries.size(); i++) { 286 String registry = (String) registries.get(i); 287 Reader reader = createReader(registry); 288 289 if (container == null) { 290 container = new Main(reader); 291 } else { 292 container.loadComponentRegistry(reader); 293 } 294 } 295 if (container == null) 296 throw new RuntimeException("No registry file specified ! [missing '--registry=' from command line!]"); 297 298 container.setProfileChecker(profileChecker); 299 300 configureRefreshMode(); 301 } 302 303 /*** 304 * @param registry 305 * @return @throws 306 * FileNotFoundException 307 */ 308 public static Reader createReader(String registry) throws FileNotFoundException { 309 if (registry.startsWith("resource://")) { 310 String resourceName = registry.substring("resource://".length()); 311 InputStream input = Kernel.class.getClassLoader().getResourceAsStream(resourceName); 312 if (input == null) 313 throw new RuntimeException("No resource found in classpath, with name " + resourceName); 314 return new InputStreamReader(input); 315 } 316 return new FileReader(registry); 317 } 318 319 /*** 320 * 321 */ 322 private void configureRefreshMode() { 323 if (refresh == null || "none".equals(refresh) || ("read".equals(refresh))) { 324 return; 325 } 326 if (("read-write".equals(refresh)) || ("read-write-nobackup".equals(refresh))) { 327 container.getRepository().setConfigurationStrategy(new PreGeneratedWrapperStrategy()); 328 if ("read-write-nobackup".equals(refresh)) 329 container.getRepository().setCreateBackups(false); 330 return; 331 } 332 throw new RuntimeException("Invalid '--refresh=" + refresh 333 + "' specified ! {valid:none,read,read-write,read-write-nobackup}"); 334 } 335 336 private void configureRefreshThread() { 337 if (refresh == null || "none".equals(refresh)) { 338 return; 339 } 340 if (("read".equals(refresh)) || ("read-write".equals(refresh)) || ("read-write-nobackup".equals(refresh))) { 341 342 Thread t = new Thread(container.new ConfigurationChecker(this.componentDir, this.refreshTime), 343 "Configuration Checker"); 344 t.setDaemon(true); 345 t.start(); 346 347 return; 348 } 349 throw new RuntimeException("Invalid '--refresh=" + refresh 350 + "' specified ! {valid:none,read,read-write,read-write-nobackup}"); 351 } 352 353 private void log(String msg) { 354 if (log4jReady) { 355 LOG.info(msg); 356 } 357 System.out.println("[kernel] " + msg); 358 } 359 360 /*** 361 * @throws ContainerException 362 * 363 */ 364 private void startContainer() throws ContainerException { 365 container.loadConfigurations(new File(this.componentDir)); 366 367 Runtime.getRuntime().addShutdownHook(new Thread("ContainerShutdown") { 368 /* 369 * (non-Javadoc) 370 * 371 * @see java.lang.Thread#run() 372 */ 373 public void run() { 374 try { 375 container.stop(); 376 } catch (ContainerException e) { 377 LOG.warn("shutdown hook failed, probably already stopped (" + e.getMessage()+')'); 378 } 379 } 380 }); 381 382 configureRefreshThread(); 383 384 container.startup(); 385 386 } 387 }

This page was automatically generated by Maven